import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';

class FavoritesService extends ChangeNotifier {
  List<String> _favoriteCities = [];

  List<String> get favoriteCities => _favoriteCities;

  FavoritesService() {
    _loadFavorites();
  }

  Future<void> _loadFavorites() async {
    final prefs = await SharedPreferences.getInstance();
    _favoriteCities = prefs.getStringList('favoriteCities') ?? [];
    notifyListeners();
  }

  Future<void> addFavorite(String city) async {
    if (!_favoriteCities.contains(city)) {
      _favoriteCities.add(city);
      final prefs = await SharedPreferences.getInstance();
      await prefs.setStringList('favoriteCities', _favoriteCities);
      notifyListeners();
    }
  }

  Future<void> removeFavorite(String city) async {
    _favoriteCities.remove(city);
    final prefs = await SharedPreferences.getInstance();
    await prefs.setStringList('favoriteCities', _favoriteCities);
    notifyListeners();
  }

  bool isFavorite(String city) {
    return _favoriteCities.contains(city);
  }
}
